<?php
class CompanyModel extends Model{

    public function add_company(){
        //select max_ID
        $max_company_id = M('company','','')->db(1,CONFIG1)->where('company_id!=888')->max('company_id');
        $max_company_id += 1;
        $_POST['company_id']=$max_company_id;

        //add company
        if(!M('company','','')->db(1,CONFIG1)->add($_POST))return false;

        //add company_admin
        $data['admin_name'] =$_POST['company_admin'];
        $data['admin_pwd']  =$_POST['company_pwd'];
        $data['admin_cid']  =$_POST['company_id'];
        $data['time_insert']=time();
        if(!M('admin','','')->db(1,CONFIG1)->add($data))return false;

        //create database
        $fp1 = fopen("./Public/Database/init_base.sql","w");
        if(!$fp1)return false;
        fwrite($fp1,"create database teamin_".$max_company_id.';');
        fclose($fp1);

        $fp3 = fopen("./Public/Database/init_data.sql","w");
        if(!$fp3)return false;
        fwrite($fp3,"use teamin_".$max_company_id.';');
        fclose($fp3);

        shell_exec('Public/Database/new_company.sh teamin_'.$max_company_id);

        return true;
    }
}
?>